package com.zhangxu.microservice.employee.web.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.Optional;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Resource
    private AeLoginInterceptor aeloginInterceptor;

    @Override
    public void configurePathMatch(@NonNull PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api/employee", c -> {
            String packageName = Optional.ofNullable(c.getPackage()).map(Package::getName).orElse("");
            return packageName.startsWith("com.zhangxu.microservice.employee.web.controller.admin")
                    && c.isAnnotationPresent(RestController.class);
        });
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //全部拦截
        String aePathPatterns = "/api/employee/ae/**";
        registry.addInterceptor(aeloginInterceptor).addPathPatterns(aePathPatterns);
    }
}
